import java.net.InetAddress;
import java.net.UnknownHostException;
import java.awt.event.*;

import javax.swing.*;


class TPlayer{
	public JLabel nickname;
	public JLabel[] cards;
	TPlayer(TInterface p, int pos){
		nickname = new JLabel();
		p.add(nickname);
		nickname.setLocation(15, 40 + 110 * pos);
		nickname.setSize(100, 30);
		cards = new JLabel[6];
		for (int i = 0; i < 6; ++i){
			cards[i] = new JLabel("");


			p.add(cards[i]);
			cards[i].setLocation(15 + 100 * i, 60 + 110 * pos);
			cards[i].setSize(100,100);
		}
	}
}

public class TInterface extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1L;
	TClient lowLevel;
	TPlayer[] playerLabels;
	JButton[] senders;
	JTextField[] textInput;
	JLabel lastLog;
	String myNickname;
	int mySeat;
	
	
	TInterface(){

		super("21");

		setSize(1000,1000);
		setLayout(null);
		playerLabels = new TPlayer[6];
		for (int i = 0; i < 6; ++i)
			playerLabels[i] = new TPlayer(this, i);
		
		textInput = new JTextField[2];
		for (int i = 0; i < 2; ++i){
			textInput[i] = new JTextField();
			textInput[i].setSize(100, 30);
			textInput[i].setLocation(15 + 150 * i, 15);
			add(textInput[i]);
		}
		textInput[0].setText("Address");
		textInput[1].setText("Nickname");
		senders = new JButton[4];
		for (int i = 0; i < 4; ++i){
			senders[i] = new JButton();
			add(senders[i]);
		}
		senders[0].setText("Connect");
		senders[0].addActionListener(this);
		senders[0].setActionCommand("Connect");
		senders[0].setBounds(310,15,120,30);
		
		
		senders[1].setText("Enter");
		senders[1].addActionListener(this);
		senders[1].setActionCommand("Enter");
		senders[1].setBounds(650, 200,120,30);
		
		senders[2].setText("More");
		senders[2].addActionListener(this);
		senders[2].setActionCommand("More");
		senders[2].setBounds(650,250,120,30);
		
		senders[3].setText("Enough");
		senders[3].addActionListener(this);
		senders[3].setActionCommand("Enough");
		senders[3].setBounds(650,300,120,30);
		
		lastLog = new JLabel("Welcome to 21");
		lastLog.setBounds(650, 100, 1000, 30);
		add(lastLog);
		lastLog.setVisible(true);
		add(lastLog);
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent arg0) {
		String s = arg0.getActionCommand();
		if (s.equals("Connect")){
			myNickname = textInput[1].getText();
			EstablishConnection(textInput[0].getText(), textInput[1].getText());
		}
		if (s.equals("Enough"))
			lowLevel.SendDecision(0);
		if (s.equals("More"))
			lowLevel.SendDecision(1);
		if (s.equals("Enter"))
			lowLevel.SendEnterGame();
	}

	
	void InterfaceModification(){
		while (true){

			TQuery current = lowLevel.Poll();
			switch (current.primary.purpose){
			case MessagesDescription.ADD_PLAYER_SUCCESS:
				System.out.print("You are added to game. Seat #");
				System.out.println(current.primary.args[0]);
				mySeat = current.primary.args[0];
				playerLabels[mySeat].nickname.setText(myNickname);
				senders[1].setVisible(false);
				break;
			case MessagesDescription.ADD_PLAYER_FAIL:
				System.out.println("No free seat. Try again later");
				break;
			case MessagesDescription.ASK_FOR_DECISION:
				System.out.println("It's you turn");
				lastLog.setText("It's your turn");
				break;
			case MessagesDescription.CARDS_OPEN:
				System.out.print("Cards of player #");
				System.out.print(current.primary.args[0]);
				for (int i = 1; i <= 6; ++i){
					playerLabels[current.primary.args[0]].cards[i-1].setText(Integer.toString(current.primary.args[i] / 10));
					System.out.print(' '); System.out.print(current.primary.args[i]);
				}
				System.out.print('\n');
				break;
			case MessagesDescription.NEW_GAME:
				System.out.println("New game starts");
				lastLog.setText("New game starts");
				for (int i = 0; i < 6; ++i)
					for (int j = 0; j < 6; ++j)
						playerLabels[i].cards[j].setText("");
				break;
			case MessagesDescription.NEXT_PLAYER:
				System.out.print("Next player #"); System.out.println(current.primary.args[0]);
				lastLog.setText("Next player is ".concat(playerLabels[current.primary.args[0]].nickname.getText()));
				break;
			case MessagesDescription.PLAYER_LEFT:
				System.out.print("Player left. #"); System.out.println(current.primary.args[0]);
				int leaver = current.primary.args[0];
				playerLabels[leaver].nickname.setText("");
				for (int i = 0; i < 6; ++i)
					playerLabels[leaver].cards[i].setText("");
				break;
			case MessagesDescription.PLAYER_X_HAS_N_CARDS:
				System.out.print("Number of cards of player #");
				System.out.print(current.primary.args[0]);
				System.out.print(' '); System.out.println(current.primary.args[1]);
				int number = current.primary.args[1];
				for (int i = 0; i < number ; ++i){
					playerLabels[current.primary.args[0]].cards[i].setText("N/A");
					System.out.print(' '); System.out.print(current.primary.args[i]);
				}

				
				break;
			case MessagesDescription.PLAYERS_NICKNAME:
				System.out.print("New player. Greet #");
				System.out.print(current.primary.args[0]);
				System.out.print(' '); System.out.println(current.secondary);
				playerLabels[current.primary.args[0]].nickname.setText(current.secondary);
				break;
			case MessagesDescription.WINNER:
				System.out.print("We have a winner! Gratz, #");
				
				System.out.println(current.primary.args[0]);
				String winner = playerLabels[current.primary.args[0]].nickname.getText();
				lastLog.setText("We have a winner! Gratz, ".concat(winner).concat("! Game will start over in seconds"));
				break;
			}
			
		}		
	}
	
	class InterfaceThread extends Thread {
		TInterface child;
		InterfaceThread(String name, TInterface ch){
			super(name);
			child = ch;
		}
		public void run(){
			child.InterfaceModification();
		}

	}
	
	void EstablishConnection(String address, String nickname){
				lowLevel = new TClient();
		try {
			lowLevel.Connect(InetAddress.getByName(address), 8840, nickname);
		} catch (UnknownHostException e) {e.printStackTrace();}
		
		new InterfaceThread("ITh", this).start();
		textInput[0].setVisible(false);
		textInput[1].setVisible(false);
		senders[0].setVisible(false);
	}


}
